/**
 * projectName: flowCloudDemo
 * fileName: WxMiniUtils.java
 * packageName: com.flowCloud.config.wx
 * date: 2021-12-03 14:10
 * copyright(c) 北京福乐云数据科技有限公司
 */
package com.flow.demo.framework.common.util.wx;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.flow.demo.framework.common.util.object.BeanUtils;
import com.flow.demo.framework.common.util.wx.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.flow.demo.framework.common.core.ErrorCodeConstants.*;

/**
 * @version: V1.0
 * @author: Pompey_hpy
 * @className: WxMiniUtils
 * @packageName: com.flowCloud.config.wx
 * @description: 微信小程序工具类
 * @data: 2021-12-03 14:10
 **/
@Slf4j
@Component
public class WxMiniUtils {

    /**
     *@title: code2Session
     *@description: TODO 登录凭证校验
     *@author: Pompey_hpy
     *@date: 2022-07-14 13:09
     *@param: [code]
     *@return: cn.hutool.json.JSONObject
     */
    public static Code2SessionVO code2Session(String code) {
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+ WxAuthConstants.MINI_APPID+"&secret="+WxAuthConstants.MINI_SECRET+"&js_code="+code+"&grant_type=authorization_code";
        String result = HttpUtil.get(url);
        if(!JSONUtil.isTypeJSON(result)){
            log.error(WECHAT_CODE_ERROR4.getMsg(),result);
        }

        Code2SessionVO bean = BeanUtils.toBean(JSONUtil.parseObj(result), Code2SessionVO.class);
        if(bean.getErrcode() == 0){
            return bean;
        }else if(bean.getErrcode() == 40029){
            log.error(WECHAT_CODE_ERROR1.getMsg());
        }else if(bean.getErrcode() == 45011){
            log.error(WECHAT_CODE_ERROR2.getMsg());
        }else if(bean.getErrcode() == 40226){
            log.error(WECHAT_CODE_ERROR3.getMsg());
        }else{
            log.error(WECHAT_CODE_ERROR4.getMsg(),result);
        }
        return null;
    }

    public static StableTokenVO stableToken () {
        String url = "https://api.weixin.qq.com/cgi-bin/stable_token";
        String body = "{\"grant_type\":\"client_credential\",\"appid\":\""+WxAuthConstants.MINI_APPID+"\",\"secret\":\""+WxAuthConstants.MINI_SECRET+"\"}";
        String result = HttpUtil.post(url,body);
        if(!JSONUtil.isTypeJSON(result)){
            log.error(WECHAT_CODE_ERROR5.getMsg(),result);
        }
        StableTokenVO bean = BeanUtils.toBean(JSONUtil.parseObj(result), StableTokenVO.class);
        if(bean.getErrcode() == 0){
            return bean;
        }else{
            log.error(WECHAT_CODE_ERROR5.getMsg(),result);
        }
        return null;
    }



    public static GetuserphonenumberDto getuserphonenumber(String code) {
        StableTokenVO stableTokenVO = stableToken();
        if (stableTokenVO == null)
            return null;
        String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+stableTokenVO.getAccess_token();
        String body = "{\"code\":\""+code+"\"}";
        String result = HttpUtil.post(url,body);
        if(!JSONUtil.isTypeJSON(result)){
            log.error(WECHAT_CODE_ERROR5.getMsg(),result);
        }
        GetuserphonenumberDto bean = BeanUtils.toBean(JSONUtil.parseObj(result), GetuserphonenumberDto.class);
        if(bean.getErrcode() == 0){
            return bean;
        }else{
            log.error(WECHAT_CODE_ERROR5.getMsg(),result);
        }
        return null;
    }


}
